/**
 * 定时任务管理初始化
 */
var Job = {
    id: "JobTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Job.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '任务id', field: 'id', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: 'spring bean名称', field: 'beanName', align: 'center', valign: 'middle', sortable: true},
        {title: '方法名', field: 'methodName', align: 'center', valign: 'middle', sortable: true},
        {title: '参数', field: 'params', align: 'center', valign: 'middle', sortable: true},
        {title: 'cron表达式', field: 'cronExpression', align: 'center', valign: 'middle', sortable: true},
        {title: '任务状态', field: 'status', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value==1){
                return "<span class='label label-warning'>暂停</span>";
        	}else{
                return "<span class='label label-success'>正常</span>";
        	}
        }},
        {title: '备注', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true},
    ];
};

/**
 * 检查是否选中
 */
Job.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Job.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加定时任务
 */
Job.openAddJob = function () {
	this.layerIndex = Sys.open('新增定时任务',Sys.backendPath + '/job/to_add',['800px', '420px']);
};

/**
 * 打开查看定时任务详情
 */
Job.openJobDetail = function () {
    if (this.check()) {
    	this.layerIndex = Sys.open('修改定时任务',Sys.backendPath + '/job/to_update/' + Job.seItem.id,['800px', '420px']);
    }
};
/**
 * 打开查看定时任务日志
 */
Job.log = function () {
	this.layerIndex = Sys.open('定时任务日志',Sys.backendPath + '/joblog', ['95%', '95%']);
};

/**
 * 删除定时任务
 */
Job.del = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/job/delete", function (data) {
	            Sys.success("删除成功!");
	            Job.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("jobId",Job.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除定时任务?",operation);
    }
};
/**
 * 立即执行任务
 */
Job.run = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/job/run", function (data) {
	            Sys.success("立即执行任务成功!");
	            Job.table.refresh();
	        }, function (data) {
	            Sys.error("立即执行任务失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("jobId",Job.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否立即执行任务?",operation);
    }
};
/**
 * 暂停定时任务
 */
Job.pause = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/job/pause", function (data) {
	            Sys.success("暂停定时任务成功!");
	            Job.table.refresh();
	        }, function (data) {
	            Sys.error("暂停定时任务失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("jobId",Job.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否暂停定时任务?",operation);
    }
};
/**
 * 恢复定时任务
 */
Job.resume = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/job/resume", function (data) {
	            Sys.success("恢复定时任务成功!");
	            Job.table.refresh();
	        }, function (data) {
	            Sys.error("恢复定时任务失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("jobId",Job.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否恢复定时任务?",operation);
    }
};

/**
 * 查询定时任务列表
 */
Job.search = function () {
    var queryData = {};
    queryData['beanName'] = $("\#beanName").val();
    queryData['methodName'] = $("\#methodName").val();
    queryData['params'] = $("\#params").val();
    queryData['cronExpression'] = $("\#cronExpression").val();
    queryData['status'] = $("\#status").val();
    queryData['description'] = $("\#description").val();
    Job.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Job.initColumn();
    var table = new BSTable(Job.id, Sys.backendPath + "/job/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSort("createDate");
    Job.table = table.init();
});
